﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL.Statistics;
using MINERP.MODEL;
using System.Data.Objects;
using System.Data.Common;
using MINERP.MODEL.FoundationModel;
using MINERP.DAL;
using MINERP.COM;

namespace MINERP.BLL.Statistics
{
    public class BillEditBLL
    {
        //StatisticsDAL statisticsDAL = new StatisticsDAL();
        //BillEditDAL billEditDAL = new BillEditDAL();
        #region 获取所有的商户信息
        /// <summary>
        /// 获取所有的商户信息
        /// </summary>
        /// <returns></returns>
        public List<Shop_Info> GetShopList()
        {
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    List<Shop> query = db.Shop.Where(p=>p.Status==1).ToList();
                    List<Shop_Info> ListShop = new List<Shop_Info>();
                    foreach (Shop record in query)
                    {
                        Shop_Info s = new Shop_Info();
                        s.ShopId = record.ShopId;
                        s.ShopName = record.shopName;
                        ListShop.Add(s);
                    }
                    return ListShop.OrderBy(p => p.ShopName).ToList();
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        public void DeleteBill(int inBillId,string inName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    var shopBillToDel = db.SHOP_Bill.FirstOrDefault(sb => sb.BillId == inBillId);
                    shopBillToDel.ShopReference.Load();
                    if (shopBillToDel != null)
                    {
                        //db.DeleteObject(shopBillToDel);
                        ChangeShopBill(0, shopBillToDel.Shop.ShopId, -(decimal)shopBillToDel.TotalPrice, "删除账单" + shopBillToDel.Shop.ShopId + "时金额补偿",
                                                  inName,db);

                        //db.DeleteObject(shopBill);


                        shopBillToDel.Status = (int)ShopBillStatus.Delete;
                    }
                    db.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }


        /// <summary>
        /// 取得账单信息按商户
        /// </summary>
        /// <param name="inShopId"></param>
        /// <param name="inStartTime"></param>
        /// <param name="inEndTime"></param>
        /// <returns></returns>
        public List<BillView> GetBillByShop(int inShopId, DateTime inStartTime, DateTime inEndTime)
        {


            List<BillView> billViewList = new List<BillView>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                int status = (int)ShopBillStatus.UnCheckOut;
                List<SHOP_Bill> listBill = db.SHOP_Bill.Where(p => p.Shop.ShopId == inShopId && p.CreateTime >= inStartTime && p.CreateTime <= inEndTime&&p.Status!=3).ToList();

                foreach (SHOP_Bill bill in listBill)
                {
                    BillView billView = new BillView();
                    billView.ShopBill = new SHOP_Bill_Info()
                    {
                        BillId = bill.BillId,
                        CreateTime = bill.CreateTime,
                        BillOrderCode = bill.BillOrderCode,
                        Status=bill.Status
                    };

                    bill.ShopReference.Load();
                    Shop shop = bill.Shop;
                    billView.ShopInfo = new Shop_Info() { ShopId = shop.ShopId, ShopName = shop.shopName };
                    billView.BillMoneyCount = GetMoneyCountByBillId(bill.BillId);

                    List<Bill_Detail> listBill_Detail = db.Bill_Detail.Where(p => p.SHOP_Bill.BillId == bill.BillId).ToList();

                    billView.status = (int)bill.Status;
                    billViewList.Add(billView);
                }
            }
            return billViewList;
        }
        /// <summary>
        /// 取得账单信息按商户
        /// </summary>
        /// <param name="inShopId"></param>
        /// <param name="inStartTime"></param>
        /// <param name="inEndTime"></param>
        /// <returns></returns>
        public List<BillView> GetBillByShop(int inId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                List<BillView> billViewList = new List<BillView>();
                // SHOP_Bill bill = billEditDAL.GetBillByID(inId);
                // int status = (int)ShopBillStatus.UnCheckOut;
                SHOP_Bill bill = db.SHOP_Bill.Where(p => p.BillId == inId).FirstOrDefault();

                BillView billView = new BillView();
                billView.ShopBill = new SHOP_Bill_Info()
                {
                    BillId = bill.BillId,
                    CreateTime = bill.CreateTime,
                    BillOrderCode = bill.BillOrderCode,
                    Status=bill.Status
                };

                bill.ShopReference.Load();
                Shop shop = bill.Shop;
                billView.ShopInfo = new Shop_Info() { ShopId = shop.ShopId, ShopName = shop.shopName };
                billView.BillMoneyCount = GetMoneyCountByBillId(bill.BillId);

                // List<Bill_Detail> listBill_Detail = billEditDAL.GetListBill_Detail(bill.BillId);
                // billView.ListBillDetailView = new List<BillDetailView>();
                //foreach (Bill_Detail detail in listBill_Detail)
                //{
                //    orderInfo order = billEditDAL.GetProductByBarCode(detail.OrderInfoCode);
                //    if (order == null)
                //    {
                //        break;
                //    }
                //    BillDetailView detailView = new BillDetailView();
                //    detailView.ShopBill = billView.ShopBill;
                //    detailView.BillDetail = new Bill_Detail_Info() { BillDetailId = detail.BillDetailId, OrderInfoCode = detail.OrderInfoCode, OrderProductName = detail.OrderProductName };
                //    detailView.Product = new OrderInfo_Info() { CtmName = order.CtmName, Height = order.Height, Width = order.Width, PageNum = order.PageNum, Remark = order.Remark, ProductNum = order.productNum  };

                //    order.Tb_ProductReference.Load();
                //    Tb_Product type = order.Tb_Product;
                //    detailView.Pro_Type = new Tb_Product_Info() { ProductName = type.ProductName };

                //    billView.ListBillDetailView.Add(detailView);
                //}
                billViewList.Add(billView);


                return billViewList;
            }

        }
        public BillView GetBillById(int inBillId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                BillView view = new BillView();
                //SHOP_Bill bill = billEditDAL.GetBill(inBillId);
                int status = (int)ShopBillStatus.UnCheckOut;
                SHOP_Bill bill = db.SHOP_Bill.Where(p => p.BillId == inBillId).FirstOrDefault();

                view.ShopBill = new SHOP_Bill_Info()
                {
                    BillId = bill.BillId,
                    CreateTime = bill.CreateTime,
                    BillOrderCode = bill.BillOrderCode
                };
                view.BillMoneyCount = GetMoneyCountByBillId(bill.BillId);
                //List<Bill_Detail> listBill_Detail = billEditDAL.GetListBill_Detail(bill.BillId);
                List<Bill_Detail> listBill_Detail = db.Bill_Detail.Where(p => p.SHOP_Bill.BillId == inBillId).ToList();
                bill.ShopReference.Load();
                Shop shop = bill.Shop;
                view.ShopInfo = new Shop_Info() { ShopId = shop.ShopId, ShopName = shop.shopName };
                return view;
            }

        }
        /// <summary>
        /// 取得账单的总金额
        /// </summary>
        /// <param name="inBillId"></param>
        /// <returns></returns>
        public double GetMoneyCountByBillId(int inBillId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                List<Bill_Detail> listBill_Detail = db.Bill_Detail.Where(p => p.SHOP_Bill.BillId == inBillId).ToList();
                double money = 0;
                foreach (Bill_Detail detail in listBill_Detail)
                {
                    //List<Bill_Detail_Detail> listBill_Detail_Detail =
                    //    billEditDAL.GetListBill_Detail_Detail(detail.BillDetailId);
                    List<Bill_Detail_Detail> listBill_Detail_Detail = db.Bill_Detail_Detail.Where(p => p.Bill_Detail.BillDetailId == detail.BillDetailId).ToList();
                    foreach (Bill_Detail_Detail list in listBill_Detail_Detail)
                    {
                        money += list.Count;
                    }
                }
                return money;
            }

        }
        /// <summary>
        /// 获取账单产品
        /// </summary>
        /// <param name="inbillId"></param>
        /// <returns></returns>
        public List<BillDetailView> GetBillDetailList(int inbillId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                List<Bill_Detail> listBill_Detail = db.Bill_Detail.Where(p => p.SHOP_Bill.BillId == inbillId).ToList();
                List<BillDetailView> viewList = new List<BillDetailView>();
                foreach (Bill_Detail detail in listBill_Detail)
                {
                    //orderInfo order = billEditDAL.GetProductByBarCode(detail.OrderInfoCode);
                    //if (order == null)
                    //{
                    //    break;
                    //}
                    BillDetailView detailView = new BillDetailView();
                    //detailView.ShopBill = billView.ShopBill;
                    detailView.BillDetail = new Bill_Detail_Info()
                    {
                        BillDetailId = detail.BillDetailId,
                        OrderInfoCode = detail.OrderInfoCode,
                        OrderProductName = detail.OrderProductName,
                        CtmName = detail.CtmName
                    };
                    //detailView.Product = new OrderInfo_Info() { CtmName = order.CtmName, Height = order.Height, Width = order.Width, PageNum = order.PageNum, Remark = order.Remark, ProductNum = order.productNum, OrderProductName = order.OrderProductName};

                    //order.Tb_ProductReference.Load();
                    //Tb_Product type = order.Tb_Product;
                    //detailView.Pro_Type = new Tb_Product_Info() { ProductName = type.ProductName };

                    viewList.Add(detailView);
                }
                return viewList;
            }
        }
        /// <summary>
        /// 获得详细列表
        /// </summary>
        /// <param name="inDetailId"></param>
        /// <returns></returns>
        public List<Bill_Detail_Detail_Info> GetBilldetaildetail(int inDetailId)
        {

            List<Bill_Detail_Detail_Info> rtnList = new List<Bill_Detail_Detail_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                //List<Bill_Detail_Detail> lists = billEditDAL.GetDetailDetail(inDetailId);
                List<Bill_Detail_Detail> lists =
                    db.Bill_Detail_Detail.Where(p => p.Bill_Detail.BillDetailId == inDetailId).ToList();
                foreach (Bill_Detail_Detail detail in lists)
                {
                    Bill_Detail_Detail_Info info = new Bill_Detail_Detail_Info()
                    {
                        Bill_Detail_DetailId = detail.Bill_Detail_DetailId,
                        Area = detail.Area,
                        Count = detail.Count,
                        DetailName = detail.DetailName,
                        Num = detail.Num,
                        Spec = detail.Spec,
                        UnitPrice = detail.UnitPrice,
                        Type = (int)detail.Type
                    };

                    rtnList.Add(info);
                }
            }
            return rtnList;

        }
        /// <summary>
        /// 取得详细信息
        /// </summary>
        /// <param name="indetaildetailId"></param>
        /// <returns></returns>
        public Bill_Detail_Detail_Info GetDetaildetailOne(int indetaildetailId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                // Bill_Detail_Detail bill = billEditDAL.GetDetailDetailOne(indetaildetailId);
                Bill_Detail_Detail
                bill = db.Bill_Detail_Detail.Where(p => p.Bill_Detail_DetailId == indetaildetailId).FirstOrDefault
                    ();
                return new Bill_Detail_Detail_Info()
                {
                    Bill_Detail_DetailId = bill.Bill_Detail_DetailId,
                    Count = bill.Count,
                    Area = bill.Area,
                    DetailName = bill.DetailName,
                    Num = bill.Num,
                    Spec = bill.Spec,
                    UnitPrice = bill.UnitPrice
                };
            }
        }

        /// <summary>
        /// 提交更改
        /// </summary>
        /// <param name="info"></param>
        public void UpdateDetailDetail(Bill_Detail_Detail_Info info, int inBillDetailId, string inName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    //Bill_Detail_Detail inBill = new Bill_Detail_Detail() { Bill_Detail_DetailId = info.Bill_Detail_DetailId, Area = info.Area, Count = info.Count, Num = info.Num, UnitPrice = info.UnitPrice, Spec = info.Spec, DetailName = info.DetailName };

                    // Bill_Detail_Detail inBill = billEditDAL.GetDetailDetailOne(info.Bill_Detail_DetailId);

                    Bill_Detail_Detail inBill =
                        db.Bill_Detail_Detail.Where(p => p.Bill_Detail_DetailId == info.Bill_Detail_DetailId).FirstOrDefault
                            ();
                    if (inBill == null)
                    {
                        inBill = new Bill_Detail_Detail();
                        //inBill.Bill_Detail = billEditDAL.GetDetailOne(inBillDetailId);
                        inBill.Bill_Detail = db.Bill_Detail.Where(p => p.BillDetailId == inBillDetailId).FirstOrDefault();
                    }
                    else
                    {
                        inBill.Bill_DetailReference.Load();
                    }
                    //inBill.Bill_Detail_DetailId = info.Bill_Detail_DetailId;
                    //inBill.Bill_Detail = billEditDAL.GetDetailOne(inBillDetailId);

                    //shopBill

                  


                    Bill_Detail billDetail = inBill.Bill_Detail;
                    billDetail.SHOP_BillReference.Load();
                    SHOP_Bill shopBill = billDetail.SHOP_Bill;
                    shopBill.ShopReference.Load();
                    shopBill.TotalPrice -= (decimal)inBill.Count;
                    ChangeShopBill(0, shopBill.Shop.ShopId, -(decimal)inBill.Count, "修改" + shopBill.BillId + "明细补偿金额", inName, db);


                    inBill.Area = info.Area;
                    inBill.Count = info.Count;
                    inBill.Num = info.Num;
                    inBill.UnitPrice = info.UnitPrice;
                    inBill.Spec = info.Spec;
                    inBill.DetailName = info.DetailName;
                    inBill.Type = info.Type;

                    shopBill.TotalPrice += (decimal)info.Count;
                    ChangeShopBill(0, shopBill.Shop.ShopId, (decimal)info.Count, "修改" + shopBill.BillId + "明细抵掉金额", inName,db);


          

                    db.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }

        }

        /// <summary>
        /// 添加账单
        /// </summary>
        /// <param name="info"></param>
        /// <param name="inBillDetailId"></param>
        /// <param name="inName"></param>
        public void AddDetailDetail(Bill_Detail_Detail_Info info, int inBillDetailId, string inName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    Bill_Detail_Detail inBill =
                        db.Bill_Detail_Detail.Where(p => p.Bill_Detail_DetailId == info.Bill_Detail_DetailId).FirstOrDefault
                            ();
                    if (inBill == null)
                    {
                        inBill = new Bill_Detail_Detail();
              
                        inBill.Bill_Detail = db.Bill_Detail.Where(p => p.BillDetailId == inBillDetailId).FirstOrDefault();
                    }
                    else
                    {
                        inBill.Bill_DetailReference.Load();
                    }

                    Bill_Detail billDetail = inBill.Bill_Detail;
                    billDetail.SHOP_BillReference.Load();
                    SHOP_Bill shopBill = billDetail.SHOP_Bill;
                    shopBill.ShopReference.Load();
                    shopBill.TotalPrice -= (decimal)inBill.Count;
                    //ChangeShopBill(0, shopBill.Shop.ShopId, -(decimal)inBill.Count, "添加" + shopBill.BillId + "明细补偿金额", inName, db);


                    inBill.Area = info.Area;
                    inBill.Count = info.Count;
                    inBill.Num = info.Num;
                    inBill.UnitPrice = info.UnitPrice;
                    inBill.Spec = info.Spec;
                    inBill.DetailName = info.DetailName;
                    inBill.Type = info.Type;

                    shopBill.TotalPrice += (decimal)info.Count;
                    ChangeShopBill(0, shopBill.Shop.ShopId, (decimal)info.Count, "添加" + shopBill.BillId + "明细抵掉金额", inName, db);

                    db.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
 
        }

        public void DelDetailDetail(int inBillDetailDetailId, string inName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    //Bill_Detail_Detail inBill = billEditDAL.GetDetailDetailOne(inBillDetailDetailId);
                    Bill_Detail_Detail inBill = db.Bill_Detail_Detail.Where(p => p.Bill_Detail_DetailId == inBillDetailDetailId).FirstOrDefault();
                    inBill.Bill_DetailReference.Load();
                    Bill_Detail billDetail = inBill.Bill_Detail;

                    billDetail.SHOP_BillReference.Load();
                    SHOP_Bill shopBill = billDetail.SHOP_Bill;
                    shopBill.ShopReference.Load();
                    Shop delShop = shopBill.Shop;
                    billDetail.Bill_Detail_Detail.Load();

                    if (billDetail.Bill_Detail_Detail.Count() > 1)
                    {

                        ChangeShopBill(0, delShop.ShopId, -(decimal)inBill.Count, "删除产品" + shopBill.BillId + "明细金额补偿", inName,db);
                        shopBill.TotalPrice -= (decimal)inBill.Count;
                        //billEditDAL.DelDetailDetail(inBill);
                        db.DeleteObject(inBill);
                        db.SaveChanges();
                    }
                    else //只有一个明细
                    {
                        //billDetail.SHOP_BillReference.Load();
                        //SHOP_Bill shopBill = billDetail.SHOP_Bill;
                        shopBill.Bill_Detail.Load();
                        if (shopBill.Bill_Detail.Count > 1)
                        {
                            shopBill.TotalPrice -= (decimal)inBill.Count;
                            ChangeShopBill(0, delShop.ShopId, -(decimal)inBill.Count, "删除产品" + shopBill.BillId + "明细金额补偿", inName,db);
                            // billEditDAL.DelDetail(billDetail);
                            db.DeleteObject(billDetail);
                            // billEditDAL.Commit();
                            db.SaveChanges();
                        }
                        else ///只有一个产品
                        {
                            ChangeShopBill(0, delShop.ShopId, -(decimal)shopBill.TotalPrice, "删除明细时删除订单金额补偿",
                                                       inName,db);
                            //billEditDAL.DelBill(shopBill);

                            db.SaveChanges();
                        }
                    }
                    tran.Commit();
                }
                catch(Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }

        }
        public void DeleteProduct(int inDetailId, string inName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    // Bill_Detail detail = billEditDAL.GetDetailOne(inDetailId);
                    Bill_Detail detail = db.Bill_Detail.Where(p => p.BillDetailId == inDetailId).FirstOrDefault();
                    detail.SHOP_BillReference.Load();
                    SHOP_Bill shopBill = detail.SHOP_Bill;
                    shopBill.ShopReference.Load();
                    shopBill.Bill_Detail.Load();
                    if (shopBill.Bill_Detail.Count() == 1) //只有一个产品删除产品就删除订单
                    {


                        ChangeShopBill(0, shopBill.Shop.ShopId, -(decimal)shopBill.TotalPrice, "删除订单" + shopBill.BillId + "时删除订单金额补偿",
                                                   inName,db);

                        shopBill.Status = (int)ShopBillStatus.Delete;
                        db.SaveChanges();
                    }
                    else
                    {
                        detail.Bill_Detail_Detail.Load();
                        List<Bill_Detail_Detail> detailDetailList = detail.Bill_Detail_Detail.ToList();
                        double deleteCount = 0;
                        foreach (Bill_Detail_Detail detailDetail in detailDetailList)
                        {
                            deleteCount += detailDetail.Count;
                        }
                        shopBill.TotalPrice -= (decimal)deleteCount;
                        ChangeShopBill(0, shopBill.Shop.ShopId, -(decimal)deleteCount, "删除" + shopBill.BillId + "的产品金额补偿", inName,db);

                        db.DeleteObject(detail);
                        db.SaveChanges();
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }

        }

        public void InsertDetail(Bill_Detail_Info inDetail, int inShopBillid)
        {
            // billEditDAL.InsertDetail(inDetail, inShopBillid);
            using (newSampleYPDBEntities DB = new newSampleYPDBEntities())
            {
                Bill_Detail detail = new Bill_Detail();
                SHOP_Bill bill = DB.SHOP_Bill.Where(p => p.BillId == inShopBillid).FirstOrDefault();
                detail.SHOP_Bill = bill;
                detail.OrderInfoCode = inDetail.OrderInfoCode;
                detail.OrderProductName = inDetail.OrderProductName;
                detail.OiRemark = inDetail.Remark;
                detail.CtmName = inDetail.CtmName;
                //  bill.TotalPrice+=
                DB.AddToBill_Detail(detail);
                DB.SaveChanges();
            }
        }
        public void UpdataDetail(Bill_Detail_Info inDetail)
        {
            //billEditDAL.UpdataDetail(inDetail);
            using (newSampleYPDBEntities DB = new newSampleYPDBEntities())
            {


                Bill_Detail detail =
                    DB.Bill_Detail.Where(p => p.BillDetailId == inDetail.BillDetailId).FirstOrDefault();
                ;
                detail.CtmName = inDetail.CtmName;
                detail.OiRemark = inDetail.Remark;
                detail.OrderInfoCode = inDetail.OrderInfoCode;
                detail.OrderProductName = inDetail.OrderProductName;
                //detail.

                DB.SaveChanges();
            }
        }
        public OrderInfo_Info GetProductByCode(string inCode)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                // orderInfo_BK order = billEditDAL.GetProductByCode(inCode);
                orderInfo_BK order = db.orderInfo_BK.FirstOrDefault(p => p.OrderProductCode == inCode);
                OrderInfo_Info info;
                if (order == null)
                {
                    return null;
                }
                else
                {
                    info = new OrderInfo_Info()
                    {
                        CtmName = order.CtmName,
                        OrderProductName = order.OrderProductCode,
                        Remark = order.Remark
                    };
                    return info;
                }
            }

        }


        public List<BillView> GetBillByShopAndMonth(int inShopId, DateTime inDate)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                List<BillView> billViewList = new List<BillView>();
                /// List<SHOP_Bill> listBill = billEditDAL.GetBillByShopAndTime(inShopId, inDate);
                int unPay = (int)ShopBillStatus.UnCheckOut;
                List<SHOP_Bill> listBill =
                    db.SHOP_Bill.Where(
                        p =>
                        p.Shop.ShopId == inShopId && p.CreateTime.Month == inDate.Month &&
                        p.CreateTime.Year == inDate.Year && p.Status == unPay).ToList();

                foreach (SHOP_Bill bill in listBill)
                {
                    BillView billView = new BillView();
                    billView.ShopBill = new SHOP_Bill_Info()
                    {
                        BillId = bill.BillId,
                        CreateTime = bill.CreateTime,
                        BillOrderCode = bill.BillOrderCode
                    };

                    bill.ShopReference.Load();
                    Shop shop = bill.Shop;
                    billView.ShopInfo = new Shop_Info() { ShopId = shop.ShopId, ShopName = shop.shopName };
                    billView.BillMoneyCount = GetMoneyCountByBillId(bill.BillId);

                    // List<Bill_Detail> listBill_Detail = billEditDAL.GetListBill_Detail(bill.BillId);
                    List<Bill_Detail> listBill_Detail = db.Bill_Detail.Where(p => p.SHOP_Bill.BillId == bill.BillId).ToList();
                    // billView.ListBillDetailView = new List<BillDetailView>();
                    //foreach (Bill_Detail detail in listBill_Detail)
                    //{
                    //    orderInfo order = billEditDAL.GetProductByBarCode(detail.OrderInfoCode);
                    //    if (order == null)
                    //    {
                    //        break;
                    //    }
                    //    BillDetailView detailView = new BillDetailView();
                    //    detailView.ShopBill = billView.ShopBill;
                    //    detailView.BillDetail = new Bill_Detail_Info() { BillDetailId = detail.BillDetailId, OrderInfoCode = detail.OrderInfoCode, OrderProductName = detail.OrderProductName };
                    //    detailView.Product = new OrderInfo_Info() { CtmName = order.CtmName, Height = order.Height, Width = order.Width, PageNum = order.PageNum, Remark = order.Remark, ProductNum = order.productNum  };

                    //    order.Tb_ProductReference.Load();
                    //    Tb_Product type = order.Tb_Product;
                    //    detailView.Pro_Type = new Tb_Product_Info() { ProductName = type.ProductName };

                    //    billView.ListBillDetailView.Add(detailView);
                    //}
                    billView.status = (int)bill.Status;
                    billViewList.Add(billView);
                }
                return billViewList;
            }
        }
        /// <summary>
        /// 转移整张账单
        /// </summary>
        /// <param name="inFromBillid"></param>
        /// <param name="inToShopId"></param>
        /// <param name="dateTime"></param>
        public void DivertBill(int inFromBillid, int inToShopId, DateTime dateTime, string strUserName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    // SHOP_Bill shopBill = billEditDAL.GetBillByID(inFromBillid);
                    SHOP_Bill
                        shopBill = db.SHOP_Bill.Where(p => p.BillId == inFromBillid).FirstOrDefault();
                    shopBill.ShopReference.Load();
                    SHOP_Bill oldBill = new SHOP_Bill();
                    oldBill.BillOrderCode = shopBill.BillOrderCode;
                    oldBill.CreateTime = shopBill.CreateTime;
                    oldBill.NeedDeliver = shopBill.NeedDeliver;
                    oldBill.ShopBalance = shopBill.ShopBalance;
                    oldBill.TotalPrice = shopBill.TotalPrice;
                    oldBill.Shop = shopBill.Shop;
                    oldBill.Status = (int)ShopBillStatus.Diverted;
                    db.AddToSHOP_Bill(oldBill);
                    Shop shopFrom = shopBill.Shop;
                    shopBill.BillOrderCode = GetCodeNext(db, inToShopId, dateTime); //修改账单序号

                    Shop shopTo = db.Shop.FirstOrDefault(p => p.ShopId == inToShopId); ;
                    shopBill.Shop = shopTo;
                    shopBill.CreateTime = dateTime;

                    ChangeShopBill(shopFrom.ShopId, inToShopId, (decimal)shopBill.TotalPrice, "", strUserName,db);
                    //修改账户余额

                    ////预付款账户自动冲抵
                    if (shopTo.CheckOutType == (int)CheckOutType.PrePay)
                    {
                        if (shopTo.Balance >= shopBill.TotalPrice)
                        {
                            shopBill.Status = (int)ShopBillStatus.CheckedOut;
                        }
                    }
                    db.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }

        }
        /// <summary>
        /// 转移产品
        /// </summary>
        /// <param name="inFromBillId"></param>
        /// <param name="inBillDetailID"></param>
        /// <param name="inToBillID"></param>
        public void DivertProduct(int inFromBillId, int inBillDetailID, int inToBillID, int inToshopID, DateTime inData)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {


                SHOP_Bill billFrom = db.SHOP_Bill.Where(p => p.BillId == inFromBillId).FirstOrDefault();
                // SHOP_Bill billTo = billEditDAL.GetBill(inToBillID);
                SHOP_Bill billTo = db.SHOP_Bill.Where(p => p.BillId == inToBillID).FirstOrDefault();

                double deDivertMoney = 0;
                //= billEditDAL.GetDetailOne(inBillDetailID);
                Bill_Detail biDetail = db.Bill_Detail.Where(p => p.BillDetailId == inBillDetailID).FirstOrDefault();
                biDetail.Bill_Detail_Detail.Load();
                List<Bill_Detail_Detail> listBill_Detail_Detail = biDetail.Bill_Detail_Detail.ToList();
                deDivertMoney = listBill_Detail_Detail.Sum(p => p.Count);
                //foreach (Bill_Detail_Detail list in listBill_Detail_Detail)
                //{
                //    money += list.Count;
                //}
                billFrom.TotalPrice -= (decimal)deDivertMoney;
                billTo.TotalPrice += (decimal)deDivertMoney;
                biDetail.SHOP_Bill = billTo;
                db.SaveChanges();
            }

        }
        public void DivertProduct(int inFromBillId, int inBillDetailID, int inToBillID, string inUserName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    // SHOP_Bill billFrom = billEditDAL.GetBillByID(inFromBillId);

                    SHOP_Bill billFrom = db.SHOP_Bill.Where(p => p.BillId == inFromBillId).FirstOrDefault();
                    SHOP_Bill billTo = db.SHOP_Bill.Where(p => p.BillId == inToBillID).FirstOrDefault();
                    double deDivertMoney = 0;
                    // Bill_Detail biDetail = billEditDAL.GetDetailOne(inBillDetailID);
                    Bill_Detail biDetail = db.Bill_Detail.Where(p => p.BillDetailId == inBillDetailID).FirstOrDefault();
                    biDetail.Bill_Detail_Detail.Load();
                    List<Bill_Detail_Detail> listBill_Detail_Detail = biDetail.Bill_Detail_Detail.ToList();
                    deDivertMoney = listBill_Detail_Detail.Sum(p => p.Count);
                    //foreach (Bill_Detail_Detail list in listBill_Detail_Detail)
                    //{
                    //    money += list.Count;
                    //}
                    billFrom.TotalPrice -= (decimal)deDivertMoney;
                    billTo.TotalPrice += (decimal)deDivertMoney;
                    biDetail.SHOP_Bill = billTo;


                    billFrom.ShopReference.Load();
                    billTo.ShopReference.Load();
                    ChangeShopBill(billFrom.Shop.ShopId, billTo.Shop.ShopId, (decimal)deDivertMoney, "",
                                               inUserName,db); //修改账户余额


                    db.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }

        }
        /// <summary>
        /// 原产品只有一个产品的 转移产品
        /// </summary>
        /// <param name="inFromBillid"></param>
        /// <param name="inShopbillID"></param>
        /// <param name="dateTime"></param>
        /// <param name="inFromDetailID"></param>
        /// <param name="inCurrBillID"></param>
        //public void DivertProduct2(int inFromBillid, int inShopID, DateTime dateTime, int inFromDetailID, int inCurrBillID)
        //{
        //    //int nextCode = billEditDAL.GetShopById(inToShopId);
        //    SHOP_Bill billFrom = billEditDAL.GetBillByID(inFromBillid);
        //    Shop shopTo = billEditDAL.GetShopById(inShopID);
        //    double deDivertMoney = 0;
        //    Bill_Detail biDetail = billEditDAL.GetDetailOne(inFromDetailID);
        //    biDetail.Bill_Detail_Detail.Load();
        //    List<Bill_Detail_Detail> listBill_Detail_Detail = biDetail.Bill_Detail_Detail.ToList();
        //    deDivertMoney = listBill_Detail_Detail.Sum(p => p.Count);


        //    billFrom.TotalPrice -= (decimal)deDivertMoney;
        //    billTo.TotalPrice += (decimal)deDivertMoney;
        //    biDetail.SHOP_Bill = billTo;



        //    billEditDAL.Commit();

        //}

        public void CreateBill(int inFromBillid, int inFromDetailID, int inShopID, DateTime dateTime, string inUserName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {

                    //SHOP_Bill billFrom = billEditDAL.GetBillByID(inFromBillid);
                    SHOP_Bill billFrom = db.SHOP_Bill.Where(p => p.BillId == inFromBillid).FirstOrDefault();
                    // List<Bill_Detail> biDetailList = billEditDAL.GetListBill_Detail(inFromDetailID);
                    // Bill_Detail biDetail = billEditDAL.GetDetailOne(inFromDetailID);

                    Bill_Detail biDetail = db.Bill_Detail.Where(p => p.BillDetailId == inFromDetailID).FirstOrDefault();
                    biDetail.Bill_Detail_Detail.Load();
                    List<Bill_Detail_Detail> listBill_Detail_Detail = biDetail.Bill_Detail_Detail.ToList();
                    decimal deDivertMoney = (decimal)listBill_Detail_Detail.Sum(p => p.Count); //修改的金额
                    SHOP_Bill newBill = new SHOP_Bill();
                    // newBill.Bill_Detail = biDetailList;
                    newBill.BillOrderCode = 1;
                    newBill.CreateTime = dateTime;
                    newBill.NeedDeliver = 1;
                    newBill.TotalPrice = deDivertMoney;
                    newBill.Status = (int)ShopBillStatus.UnCheckOut;
                    // Shop shopTo = billEditDAL.GetShopById(inShopID);
                    Shop shopTo = db.Shop.FirstOrDefault(p => p.ShopId == inShopID);
                    newBill.Shop = shopTo;
                    //AddShopBill(newBill, inFromDetailID);
                    Bill_Detail fromDetail = db.Bill_Detail.FirstOrDefault(p => p.BillDetailId == inFromDetailID);
                    newBill.Bill_Detail.Add(fromDetail);

                    billFrom.ShopReference.Load();
                    billFrom.TotalPrice -= (decimal)deDivertMoney;
                    ChangeShopBill(billFrom.Shop.ShopId, inShopID, (decimal)deDivertMoney, "", inUserName,db);
                    //修改账户余额

                    ////预付款账户自动冲抵
                    if (shopTo.CheckOutType == (int)CheckOutType.PrePay)
                    {
                        if (shopTo.Balance >= (decimal)deDivertMoney)
                        {
                            newBill.Status = (int)ShopBillStatus.CheckedOut;
                        }
                    }
                    db.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 修改转移账单修改相应的商户金额
        /// </summary>
        /// <param name="inFromShopId"></param>
        /// <param name="inToShopId"></param>
        /// <param name="inMoney"></param>
        /// <param name="strRemark"></param>
        /// <returns></returns>
        public int ChangeShopBill(int inFromShopId, int inToShopId, decimal inMoney, string strRemark, string inStrUserName, newSampleYPDBEntities db)
        {
            try
            {
                Shop shopFrom = db.Shop.FirstOrDefault(p => p.ShopId == inFromShopId);
                Shop shopTo = db.Shop.FirstOrDefault(p => p.ShopId == inToShopId);
               
                if (inFromShopId != 0 && inToShopId != 0) //转移账单
                {
                    //业务员账户处理----开始
                    SalesmanBalance salesmanfrom = db.SalesmanBalance.FirstOrDefault(s => s.SalesmanName == shopFrom.Salesman);
                    if (salesmanfrom != null)
                    {
                        salesmanfrom.Balance -= inMoney;
                    }
                    SalesmanBalance salesmanTo = db.SalesmanBalance.FirstOrDefault(s => s.SalesmanName == shopTo.Salesman);
                    if (salesmanTo != null)
                    {
                        salesmanTo.Balance += inMoney;
                    }
                    //业务员账户处理----结束
                    shopFrom.Balance = shopFrom.Balance + inMoney; //转出账单加上相应的金额
                    ShopBlanceCrushRecord recordFrom = new ShopBlanceCrushRecord();
                    recordFrom.CrushMoney = inMoney;
                    recordFrom.CrushTime = DateTime.Now;
                    recordFrom.Operation = (int)CountOperationType.EditBill;
                    recordFrom.Overage = (decimal)shopFrom.Balance;
                    recordFrom.Remark = "移走账单(产品)金额补偿";
                    recordFrom.Shop = shopFrom;
                    recordFrom.Operator = inStrUserName;
                    db.AddToShopBlanceCrushRecord(recordFrom);
                    shopTo.Balance = shopTo.Balance - inMoney; //转如账单减去相应的金额
                    ShopBlanceCrushRecord recordTo = new ShopBlanceCrushRecord();
                    recordTo.CrushMoney = -inMoney;
                    recordTo.CrushTime = DateTime.Now;
                    recordTo.Operation = (int)CountOperationType.DiChong;
                    recordTo.Overage = (decimal)shopTo.Balance;
                    recordTo.Remark = "添加账单(产品)金额抵扣";
                    recordTo.Shop = shopTo;
                    recordTo.Operator = inStrUserName;
                    db.AddToShopBlanceCrushRecord(recordTo);

                }
                else if (inFromShopId == 0 && inToShopId != 0) //修改账单
                {
                    //业务员账户处理----开始
                    SalesmanBalance salesmanTo = db.SalesmanBalance.FirstOrDefault(s => s.SalesmanName == shopTo.Salesman);
                    if (salesmanTo != null)
                    {
                        salesmanTo.Balance -= inMoney;
                    }
                    //业务员账户处理----结束
                    shopTo.Balance = shopTo.Balance - inMoney; //修改明细
                    ShopBlanceCrushRecord recordTo = new ShopBlanceCrushRecord();
                    recordTo.CrushMoney = -inMoney;
                    recordTo.CrushTime = DateTime.Now;
                    if (-inMoney > 0)
                    {
                        recordTo.Operation = (int)CountOperationType.EditBill;
                    }
                    else
                    {
                        recordTo.Operation = (int)CountOperationType.DiChong;
                    }
                   
                    recordTo.Overage = (decimal)shopTo.Balance;
                    recordTo.Remark = strRemark;
                    recordTo.Shop = shopTo;
                    recordTo.Operator = inStrUserName;
                    db.AddToShopBlanceCrushRecord(recordTo);

                }
                else
                {

                }
                //db.SaveChanges();
                return 0;
            }
            catch (Exception exception)
            {
                throw exception;
                // return -1;

            }
           
        }
        public int GetCodeNext(newSampleYPDBEntities db,int inShopID, DateTime inDate)
        {
            //using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            //{


                List<SHOP_Bill> billList =
                    db.SHOP_Bill.Where(
                        p =>
                        p.Shop.ShopId == inShopID && p.CreateTime.Year == inDate.Year &&
                        p.CreateTime.Month == inDate.Month).ToList();
                SHOP_Bill bill = new SHOP_Bill();
                if (billList.Count == 0)
                {
                    return 1;
                }
                else
                {
                    return billList.Max(p => p.BillOrderCode) + 1;
                }
            //}
        }

        //public void AddShopBill(SHOP_Bill inShopBill, int inBillDetailId)
        //{
        //    using (newSampleYPDBEntities db = new newSampleYPDBEntities())
        //    {
        //        foreach (var a in db.Bill_Detail.Where(p => p.BillDetailId == inBillDetailId))
        //        {
        //            inShopBill.Bill_Detail.Add(db.Bill_Detail.Where(a.BillDetailId==inBillDetailId));
        //        }
        //        db.AddToSHOP_Bill(inShopBill);
        //    }

        //}
        /// <summary>
        /// 创建整个账单的退单
        /// </summary>
        /// <param name="inBillID"></param>
        /// <param name="inStrUserName"></param>
        /// <returns></returns>
        public int CreateOutReturn(int inBillID, string inStrUserName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    SHOP_Bill sbill = db.SHOP_Bill.FirstOrDefault(p => p.BillId == inBillID);
                    sbill.ShopReference.Load();
                    Shop currShop = sbill.Shop;

                    sbill.Bill_Detail.Load();
                    List<Bill_Detail> bill_Detail = sbill.Bill_Detail.ToList();

                    foreach (Bill_Detail bd in bill_Detail)
                    {
                        bd.Bill_Detail_Detail.Load();
                    }
                    SHOP_Bill snbill = new SHOP_Bill();
                    snbill.Shop = currShop;


                    currShop.Balance = currShop.Balance + sbill.TotalPrice;//将账单金额补偿给商户

                    //业务员账户操作
                    SalesmanBalance salesman = db.SalesmanBalance.FirstOrDefault(s=>s.SalesmanName ==currShop.Salesman);
                    if (salesman != null)
                    {
                        salesman.Balance += (decimal)sbill.TotalPrice;
                    }


                    snbill.ShopBalance = (decimal)currShop.Balance;

                    snbill.TotalPrice = -sbill.TotalPrice;


                    ShopBlanceCrushRecord recordNew = new ShopBlanceCrushRecord();
                    recordNew.CrushMoney = (decimal)sbill.TotalPrice;
                    recordNew.CrushTime = DateTime.Now;
                    recordNew.Operation = (int)CountOperationType.EditBill;
                    recordNew.Overage = (decimal)currShop.Balance;
                    recordNew.Remark = "退单金额补偿";
                    recordNew.Shop = currShop;
                    recordNew.Operator = inStrUserName;
                    db.AddToShopBlanceCrushRecord(recordNew);


                    snbill.CreateTime = DateTime.Now;
                    snbill.Status = (int)ShopBillStatus.CheckedOut;
                    snbill.NeedDeliver = 0;
                    //snbill.WriteIntoDeliveryInfo
                    snbill.BillOrderCode = GetCodeNext(db,currShop.ShopId, DateTime.Now);
                    List<Bill_Detail> bdList = new List<Bill_Detail>();
                    foreach (Bill_Detail bd in bill_Detail)
                    {
                        Bill_Detail newBd = new Bill_Detail();
                        newBd.CtmName = bd.CtmName;
                        newBd.IsCheckOut = bd.IsCheckOut;
                        newBd.OiRemark = bd.OiRemark;
                        newBd.OrderInfoCode = bd.OrderInfoCode;
                        newBd.SHOP_Bill = snbill;
                        newBd.OrderProductName = bd.OrderProductName + "-退单";
                        //newBd.
                        bdList.Add(newBd);
                        foreach (Bill_Detail_Detail bdd in bd.Bill_Detail_Detail)
                        {
                            Bill_Detail_Detail newbdd = new Bill_Detail_Detail();
                            newbdd.Area = -bdd.Area;
                            newbdd.Bill_Detail = newBd;
                            newbdd.Count = -bdd.Count;
                            newbdd.DetailName = bdd.DetailName;
                            newbdd.Num = -bdd.Num;
                            newbdd.Spec = bdd.Spec;
                            newbdd.UnitPrice = bdd.UnitPrice;
                            newbdd.Type = bdd.Type;
                        }
                    }
                    db.AddToSHOP_Bill(snbill);
                    db.SaveChanges();
                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                    return -1;
                }
                return 1;
            }
        }
        /// <summary>
        /// 创建产品的退单
        /// </summary>
        /// <param name="inBillDetailid"></param>
        /// <param name="inStrUserName"></param>
        /// <returns></returns>
        public int CreateOutReturn2( int inBillDetailid, string inStrUserName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (db.Connection.State != System.Data.ConnectionState.Open)
                {
                    db.Connection.Open();
                }
                System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    Bill_Detail oldDetail = db.Bill_Detail.FirstOrDefault(p => p.BillDetailId == inBillDetailid);
                    oldDetail.SHOP_BillReference.Load();
                    SHOP_Bill sbill = oldDetail.SHOP_Bill ;
                    sbill.ShopReference.Load();
                    Shop currShop = sbill.Shop;

                    sbill.Bill_Detail.Load();
                    Bill_Detail bill_Detail = sbill.Bill_Detail.FirstOrDefault(p => p.BillDetailId == inBillDetailid);

                    bill_Detail.Bill_Detail_Detail.Load();
                    SHOP_Bill snbill = new SHOP_Bill();
                    snbill.Shop = currShop;
                    //snbill.ShopBalance = sbill.ShopBalance + sbill.TotalPrice;//获取产品的金额
                    //snbill.TotalPrice = -sbill.TotalPrice;
                    snbill.CreateTime = DateTime.Now;
                    snbill.Status = (int)ShopBillStatus.CheckedOut;
                    snbill.NeedDeliver = 0;

                   

                    //snbill.WriteIntoDeliveryInfo
                    snbill.BillOrderCode = GetCodeNext(db,currShop.ShopId, DateTime.Now);
                    Bill_Detail newBd = new Bill_Detail();
                    newBd.CtmName = bill_Detail.CtmName;
                    newBd.IsCheckOut = bill_Detail.IsCheckOut;
                    newBd.OiRemark = bill_Detail.OiRemark;
                    newBd.OrderInfoCode = bill_Detail.OrderInfoCode;
                    newBd.SHOP_Bill = snbill;
                    newBd.OrderProductName = bill_Detail.OrderProductName + "-退单";
                    //newBd.
                    double tatalproductprice = 0;
                    foreach (Bill_Detail_Detail bdd in bill_Detail.Bill_Detail_Detail)
                    {
                        Bill_Detail_Detail newbdd = new Bill_Detail_Detail();
                        newbdd.Area = -bdd.Area;
                        newbdd.Bill_Detail = newBd;
                        newbdd.Count = -bdd.Count;
                        newbdd.DetailName = bdd.DetailName;
                        newbdd.Num = -bdd.Num;
                        newbdd.Spec = bdd.Spec;
                        newbdd.UnitPrice = bdd.UnitPrice;
                        newbdd.Type = bdd.Type;
                        tatalproductprice += bdd.Count;
                    }
                    currShop.Balance = currShop.Balance + (decimal)tatalproductprice;

                    //业务员账户操作
                    SalesmanBalance salesman =db.SalesmanBalance.FirstOrDefault(s=>s.SalesmanName ==currShop.Salesman);
                    if (salesman != null)
                    {
                        salesman.Balance += (decimal)tatalproductprice;
                    }
                    

                    snbill.ShopBalance = (decimal)currShop.Balance;
                    snbill.TotalPrice = -(decimal)tatalproductprice;
                    ShopBlanceCrushRecord recordNew = new ShopBlanceCrushRecord();
                    recordNew.CrushMoney = (decimal)tatalproductprice;
                    recordNew.CrushTime = DateTime.Now;
                    recordNew.Operation = (int)CountOperationType.EditBill;
                    recordNew.Overage = (decimal)currShop.Balance;
                    recordNew.Remark = "退单金额补偿";
                    recordNew.Shop = currShop;
                    recordNew.Operator = inStrUserName;
                    db.AddToShopBlanceCrushRecord(recordNew);
                    db.AddToSHOP_Bill(snbill);
                    db.SaveChanges();
                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                    return -1;
                }
                return 1;
            }
        }

        //创建新的账单title
        public int CreateShopBillTitle(int shopId, string inStrUserName)
        {
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Shop currShop = db.Shop.FirstOrDefault(p => p.ShopId == shopId);
                    SHOP_Bill shopBill = new SHOP_Bill();
                    shopBill.BillOrderCode = GetCodeNext(db, shopId, DateTime.Now);
                    shopBill.CreateTime = DateTime.Now;
                    shopBill.NeedDeliver = 0;
                    shopBill.Shop = currShop;
                    shopBill.ShopBalance = (decimal)currShop.Balance;
                    shopBill.Status = 0;
                    shopBill.TotalPrice = 0;
                    shopBill.WriteIntoDeliveryInfo = 0;
                    db.AddToSHOP_Bill(shopBill);
                    db.SaveChanges();
                    return shopBill.BillId;
                }
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

    }
}
